﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using QualityEvaluatingTools.manager;
using QualityEvaluatingTools.model;

namespace QualityEvaluatingTools.helper
{
    public class XmlHelper
    {

        #region 公共方法

        /// <summary>
        /// 获取基础数据
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static XDocument GetBasicRuleDoc(string path)
        {
            XDocument xDocument = XDocument.Load(path);

            return xDocument;
        }

        /// <summary>
        /// 获取综合评测的项规则xml'
        /// </summary>
        /// <param name="evaluationType">综合评测类型</param>
        /// <param name="baseDoc">基础doc</param>
        /// <returns></returns>
        public static IEnumerable<XElement> GetEvaluationXmlDoc(EvaluationType evaluationType, XDocument baseDoc)
        {
            int evaluationId = (int)evaluationType;
            var ad = from a in baseDoc.Elements("evaluating").Elements("evaluatingitem")
                     where a.Attribute("id").Value == evaluationId.ToString()
                     select a;
            return ad;
        }

        /// <summary>
        /// 获取学生选项的得分
        /// </summary>
        /// <param name="answer">学生答案</param>
        /// <param name="xmlDoc">文档结构</param>
        /// <returns></returns>
        public static int GetStuSelectScore(StuAnswer answer, IEnumerable<XElement> xmlDoc)
        {

            var subjectDoc = from b in xmlDoc.Elements("subjects").Elements("subject")
                             where
                                 b.Attribute("examid").Value == answer.ExamId.ToString()
                             select b;

            var subjectScore = from temp in subjectDoc.Elements("options").Elements("option")
                               where
                                   temp.Attribute("name").Value == answer.AnswerContent
                               select temp;

            if (subjectScore != null)
            {
                foreach (var d in subjectScore)
                {
                    return Convert.ToInt32(d.Value);
                }
            }
            return 0;
        }

        /// <summary>
        /// 获取学生职业性格测试结果
        /// </summary>
        /// <param name="answer">学生答案</param>
        /// <param name="xmlDoc">文档结构</param>
        /// <returns></returns>
        public static string GetStuOccupationalCharacterTestResult(StuAnswer answer, IEnumerable<XElement> xmlDoc)
        {

            var subjectDoc = from b in xmlDoc.Elements("subjects").Elements("subject")
                             where
                                 b.Attribute("examid").Value == answer.ExamId.ToString()
                             select b;

            var subjectSelect = from temp in subjectDoc.Elements("options").Elements("option")
                               where
                                   temp.Attribute("name").Value == answer.AnswerContent
                               select temp;

            if (subjectSelect != null)
            {
                foreach (var d in subjectSelect)
                {
                    return d.Value;
                }
            }
            return null;
        }

        #endregion

        #region 私有辅助方法

        /// <summary>
        /// 读取XML
        /// </summary>
        public static void ReadFromXml()
        {
            string path = @"d:\XMLFile1.xml";
            XDocument xdoc = XDocument.Load(path);

            //Console.WriteLine(xdoc);
            Console.WriteLine("start...");
            var ad = from a in xdoc.Elements("evaluating").Elements("evaluatingitem").Elements("subjects").Elements("subject")

                     where a.Attribute("examid").Value == "1"
                     select a;

            var c = from b in ad.Elements("Options")
                    where
                        b.Attribute("name").Value == "B"
                    select b;

            foreach (var a in ad)
            {
                Console.WriteLine(a);

            }
            foreach (var d in c)
            {
                Console.Write(d.Value);
            }
        }

        #endregion

    }
}
